﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MaterialSkin.Controls;
using S7.Net;
using thinger.DataConvertLib;
using User_manage;
using static User_manage.User_Lgon;
using DataType = S7.Net.DataType;


namespace Station_interface
{
    public partial class Station_Interface : UserControl
    {
        #region //属性
        public string Quantity //总产量
        {
            set { uiTextBox3.Text = value; }
            get { return uiTextBox3.Text; }
        }

        public string OK_quantity //OK数量
        {
            set { uiTextBox4.Text = value; }
            get { return uiTextBox4.Text; }
        }

        public string NG_quantity //NG数量
        {
            set { uiTextBox5.Text = value; }
            get { return uiTextBox5.Text; }
        }

        public string Cycle //CT/节拍
        {
            set { uiTextBox1.Text = value; }
            get { return uiTextBox1.Text; }
        }

        public string Bar_sn //生产条码
        {
            set { uiTextBox2.Text = value; }
            get { return uiTextBox2.Text; }
        }

        public string IP //IP
        {
            set { label_IP.Text = value; }
            get { return label_IP.Text; }
        }

        public string Station_name //Station_name
        {
            set { label2.Text = value; }
            get { return label2.Text; }
        }

        public bool led_lamp1_State //Station_name
        {
            set { led_lamp1.State = value; }
            get { return led_lamp1.State; }
        }
        public bool led_lamp2_State //Station_name
        {
            set { led_lamp2.State = value; }
            get { return led_lamp2.State; }
        }
        public bool led_lamp3_State //Station_name
        {
            set { led_lamp3.State = value; }
            get { return led_lamp3.State; }
        }
        public bool led_lamp4_State //Station_name
        {
            set { led_lamp4.State = value; }
            get { return led_lamp1.State; }
        }

        public ListBox ListBox_info //
        {
            set { listBox1 = value; }
            get { return listBox1; }
        }

        public ListBox Aliarm_info //
        {
            set { Aliarm_text = value; }
            get { return Aliarm_text; }
        }

        #endregion


        public Station_Interface()
        {
            InitializeComponent();
            SetStyle(ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, value: true);
            UpdateStyles();
        }

        private void StationControl_Load(object sender, EventArgs e)
        {
            ;
        }

        /// <summary>
        /// 画边框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void materialCard1_Paint(object sender, PaintEventArgs e)
        {
            Rectangle buf = new Rectangle() { X = 0, Y = 0, Width = this.Width, Height = this.Height };
            FrameRoundRectangle(buf, e.Graphics, 10, Color.Black);
        }

        /// <summary>
        /// 圆角矩形边框
        /// </summary>
        /// <param name="rectangle">原始矩形</param>
        /// <param name="g">Graphics 对象</param>
        /// <param name="radius">圆角半径</param>
        /// <param name="backColor">颜色</param>
        public void FrameRoundRectangle(Rectangle rectangle, Graphics g, int radius, Color backColor)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            if (radius == 0)
            {
                g.DrawRectangle(new Pen(backColor), rectangle);
            }
            else
            {
                g.DrawPath(new Pen(backColor), DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - 2, rectangle.Height - 1, radius));
            }


            //Brush brush = new SolidBrush(backColor);
            //g.FillEllipse(brush, 2, 2, 50, 50);
        }

        public GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
        {
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(x, y, radius, radius, 180, 90);
            gp.AddArc(x + width - radius, y, radius, radius, 270, 90);
            gp.AddArc(x + width - radius, y + height - radius, radius, radius, 0, 90);
            gp.AddArc(x, y + height - radius, radius, radius, 90, 90);
            gp.CloseAllFigures();
            return gp;
        }

        //参数设置事件
        SetdataForm _setdataForm;
        private void button2_Click(object sender, EventArgs e)
        {
            if (User_Level != H_Level.管理员)
            {
                MessageBox.Show("权限错误，请切换为管理员权限", "错误提示");
                return;
            }

            _setdataForm = new SetdataForm(this);
            _setdataForm.ShowDialog();
            _setdataForm.Dispose();
            //Infobuf = Deserialize<DataTable>(Application.StartupPath + "\\Config\\" + Station._PlcName + "_Info.config");
            ;
        }

        /// <summary>
        /// 反序列
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="file_path"></param>
        /// <returns></returns>
        public T Deserialize<T>(string file_path)
        {
            try
            {
                using (FileStream fs = new FileStream(file_path, FileMode.Open))
                {
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    return (T)binaryFormatter.Deserialize(fs);
                }
            }
            catch
            {
                return default(T);
            }
        }

    }
}

/// <summary>
/// 多线程里UI控件显示
/// </summary>
public static class ControlExtention
{
    public delegate void InvokeHandler();
    public static void SafeInvoke(this Control control, InvokeHandler handler)
    {
        if (control.InvokeRequired)
        {
            control.Invoke(handler);
        }
        //else
        //{
        //    handler();
        //}
    }
}

public class MessageBoxManager
{
    private const int WM_SETFONT = 0x30;

    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    extern private static IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

    public static void SetMessageBoxFont(Font font)
    {
        SendMessage(GetMsgBoxHandle(), WM_SETFONT, font.ToHfont(), (IntPtr)1);
    }

    public static void ResetMessageBoxFont()
    {
        SendMessage(GetMsgBoxHandle(), WM_SETFONT, IntPtr.Zero, (IntPtr)1);
    }

    private static IntPtr GetMsgBoxHandle()
    {
        IntPtr hwndDesktop = GetDesktopWindow();
        IntPtr hwndMsgBox = FindWindowEx(hwndDesktop, IntPtr.Zero, "#32770", null);
        return hwndMsgBox;
    }

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern IntPtr GetDesktopWindow();

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
}